package org.aristotle.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.aristotle.user.model.entity.Permission;
import org.aristotle.user.service.IPermissionService;
import org.aristotle.user.model.vo.PermissionVO;

@RestController
@RequestMapping("permission")
@Api(value="权限controller",tags={"权限操作接口"})
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "权限id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "name", paramType = "query", value = "权限名", dataType = "String", required = false),
            @ApiImplicitParam(name = "identifier", paramType = "query", value = "权限标识符", dataType = "String", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<PermissionVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "identifier",required = false) String identifier,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<PermissionVO> result = permissionService.queryPageList(
            id,
            name,
            identifier,
            current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "权限id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        PermissionVO result = permissionService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(permissionService.list(),PermissionVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody PermissionVO permissionVO) {
        Permission permission = AristotleBeanUtils.transitionBean(permissionVO, Permission::new);
        permission.setId(null);
        boolean result = permissionService.save(permission);
        if (result) {
            this.permissionService.refreshAuthRule();
        }
        return ResponseEntity.addOk(permissionVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody PermissionVO permissionVO) {
        if (permissionVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        Permission permission = AristotleBeanUtils.transitionBean(permissionVO, Permission::new);
        boolean result = permissionService.updateById(permission);
        if (result) {
            this.permissionService.refreshAuthRule();
        }
        return ResponseEntity.updateOk(permissionVO);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "权限id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        Boolean result = permissionService.removeById(id);
        if (result) {
            this.permissionService.refreshAuthRule();
        }
        return ResponseEntity.deleteOk(id);
    }

}
